/*
* MyWidget.cpp
*
* Copyright(C) 2009, Лущик Павел aka nanoo
*
* This code is lisenced under the GPL version 2. For details see 
* LISENCE.txt
*/

#include"MyWidget.h"

MyWidget::MyWidget(QWidget *parent): QWidget(parent)
{
	buttons_layout = new QGridLayout();
	layout = new QGridLayout();
	
	hex_table = new QLabel("0000 0\n0001 1\n0010 2\n0011 3\n0100 4\n0101 5\n0110 6\n0111 7\n1000 8\n1001 9\n1010 A\n1011 B\n1100 C\n1101 D\n1110 E\n1111 F");
	
	Xreg = new StorageContaner("Xreg",this);
	Yreg = new StorageContaner("Yreg",this);
	
	layout->addWidget(Yreg,0,0);
	layout->addWidget(Xreg,0,1);
	
	b_plus = new QPushButton("Y+X -> X",this);
	b_plus->setToolTip(tr("Summation"));
	b_minus = new QPushButton("Y-X -> X",this);
	b_minus->setToolTip(tr("Substruction"));
	b_flip = new QPushButton("Y <-> X",this);
	b_flip->setToolTip(tr("Flip resigters"));
	b_and = new QPushButton("Y and X -> X",this);
	b_and->setToolTip(tr("Logical <b>and</b>"));
	b_or = new QPushButton("Y or X -> X",this);
	b_or->setToolTip(tr("Logical <b>or</b>"));
	b_xor = new QPushButton("Y xor X -> X",this);
	b_xor->setToolTip(tr("Logical <b>xor</b>"));
	
	buttons_layout->addWidget(b_plus,0,0);
	buttons_layout->addWidget(b_minus,0,1);
	buttons_layout->addWidget(b_flip,0,2);
	buttons_layout->addWidget(b_and,1,0);
	buttons_layout->addWidget(b_or,1,1);
	buttons_layout->addWidget(b_xor,1,2);
	
	connect(b_plus,SIGNAL(clicked()),this,SLOT(plus()));
	connect(b_minus,SIGNAL(clicked()),this,SLOT(minus()));
	connect(b_flip,SIGNAL(clicked()),this,SLOT(flip()));
	connect(b_and,SIGNAL(clicked()),this,SLOT(l_and()));
	connect(b_or,SIGNAL(clicked()),this,SLOT(l_or()));
	connect(b_xor,SIGNAL(clicked()),this,SLOT(l_xor()));
	
	layout->addLayout(buttons_layout,2,0,1,2);
	layout->addWidget(hex_table,0,3,3,1);
	
	setLayout(layout);
	setFixedSize(sizeHint());
}
void MyWidget::plus()
{
	Xreg->setValue(Yreg->value()+Xreg->value());
}

void MyWidget::minus()
{
	Xreg->setValue(Yreg->value()-Xreg->value());
}

void MyWidget::flip()
{
	int tmp;
	tmp = Xreg->value();
	Xreg->setValue(Yreg->value());
	Yreg->setValue(tmp);
}

void MyWidget::l_and()
{
	Xreg->setValue(Yreg->value() & Xreg->value());
}

void MyWidget::l_or()
{
	Xreg->setValue(Yreg->value() | Xreg->value());
}

void MyWidget::l_xor()
{
	Xreg->setValue(Yreg->value() ^ Xreg->value());
}
